package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.consts.SysDictTypeKeyConst;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.User;
import com.tongzhan.entity.party.ActionSite;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 活动中心-申请场地
 * @author htmic
 * @date 2020/7/29
 */
@Service
public class ActionSiteService extends BaseShareService {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 添加 申请场地
     * @param actionSite
     * @return
     */
    public long addActionSite(ActionSite actionSite) {
        actionSite.setAuditStatus(0);
        actionSite.setIs_del(0);
        actionSite.setAddTime(new Date());
        User userById = findUserById(actionSite.getAddUserId());
        if(userById!=null){
            actionSite.setAddUserName(userById.getUserName());
        }
        actionSite.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        actionSite.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = actionSiteRepository.insertSelective(actionSite);
        if(l>0){
            //添加角标
            markPromptService.add(new MarkPrompt(1, actionSite.getId()),"13");
            return  1;
        }
        return 0;
    }


    /**
     * 活动列表
     * @param page
     * @param size
     * @return
     */
    public List actionSiteList(Integer page, Integer size, String userId, String year_and_month) {
        List<Map<String, Object>> list = new ArrayList<>();

        //处理时间查询
        Date startDate = null;
        Date endDate = null;
        if (year_and_month.length()>4){
            //查询年月 YYYY-MM
            Date date = DateSwitcher.string2Date(year_and_month, "yyyy-MM");
            startDate = DateSwitcher.getMonthFirstDay(date);
            endDate = DateSwitcher.getMonthLastDay(date);
        }else{
            //查询年 YYYY
            Date date = DateSwitcher.string2Date(year_and_month, "yyyy");
            startDate = DateSwitcher.getYearFirstDay(date);
            endDate = DateSwitcher.getYearLastDay(date);
        }

        //党派工商科
        Query qw = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("predictTime").gte(startDate).lte(endDate));
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "addTime");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionSite> pageList1 = actionSiteRepository.findPage(qw, of);
        if (!CollectionUtils.isEmpty(pageList1.getContent())) {
            pageList1.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_ACTION_TYPE, item.getActionTypeId()));
                item.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, item.getPartyTypeId()));
                item.setActionAddressName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_SITE_TYPE, item.getAction_address()));
            });
        }

        //党派
        Query qw2 = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("addUserId").is(userId).and("predictTime").gte(startDate).lte(endDate));
        PageRequest of2 = PageRequest.of(page, size, Sort.Direction.DESC, "addTime");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw2.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw2.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionSite> pageList2 = actionSiteRepository.findPage(qw2, of2);
        if (!CollectionUtils.isEmpty(pageList2.getContent())) {
            pageList2.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_ACTION_TYPE, item.getActionTypeId()));
                item.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, item.getPartyTypeId()));
                item.setActionAddressName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_SITE_TYPE, item.getAction_address()));
            });
        }

        //场地人员
        Query qw3 = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("predictTime").gte(startDate).lte(endDate));
        PageRequest of3 = PageRequest.of(page, size, Sort.Direction.DESC, "addTime");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw3.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw3.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionSite> pageList3 = actionSiteRepository.findPage(qw3, of3);
        if (!CollectionUtils.isEmpty(pageList3.getContent())) {
            pageList3.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_ACTION_TYPE, item.getActionTypeId()));
                item.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, item.getPartyTypeId()));
                item.setActionAddressName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_SITE_TYPE, item.getAction_address()));
            });
        }

        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        map1.put("gskList", pageList1.getContent());
        map1.put("total", pageList1.getTotalElements());
        map2.put("dpList", pageList2.getContent());
        map2.put("total", pageList2.getTotalElements());
        map3.put("cdryList", pageList3.getContent());
        map3.put("total", pageList3.getTotalElements());

        list.add(map1);
        list.add(map2);
        list.add(map3);
        return list;
    }


    /**
     * 详情
     * @param id
     * @return
     */
    public ActionSite getDetail(String id, String userId) {
        ActionSite actionSite = actionSiteRepository.findByPrimaryKey(id);
        if (actionSite != null) {
            actionSite.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_ACTION_TYPE, actionSite.getActionTypeId()));
            actionSite.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, actionSite.getPartyTypeId()));
            actionSite.setActionAddressName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_SITE_TYPE, actionSite.getAction_address()));
        }
        markPromptService.read(id,userId);
        return actionSite;
    }


    /**
     * 审核
     * @param id
     * @param type 0是不同意 1是同意
     * @param userId 审核人id
     * @return
     */
    public long auditSite(String id, Integer type,String auditContent, String userId) {
        ActionSite actionSite = actionSiteRepository.findByPrimaryKey(id);
        if(actionSite!=null){
            User user = findUserById(userId);
            if(user!=null){
                actionSite.setAuditTime(new Date());
                actionSite.setAuditUserId(userId);
                actionSite.setAuditUserName(user.getUserName());
            }
            actionSite.setAuditStatus(type==0?2:1);//审核结果   0:未审核、1:同意 2:不同意
            actionSite.setAuditContent(auditContent);
            long l = actionSiteRepository.updateByPrimaryKeySelective(actionSite);
            if (l >= 0) {
                if(actionSite.getAuditStatus()==1){//0:未审核、1:同意 2:不同意
                    //添加角标
                    markPromptService.add(new MarkPrompt(2, actionSite.getId()),"12");
                }
                return 1;
            } else {
                return 0;
            }
        }
        return 0;
    }
}
